package com.pur.zsms.financing.web;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.cglib.beans.BeanCopier;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.pur.zsms.check.vo.CheckVo;
import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.FileUtill;
import com.pur.zsms.cost.po.CostPo;
import com.pur.zsms.cost.po.CostSubPo;
import com.pur.zsms.cost.service.ICostService;
import com.pur.zsms.financing.service.IFinancingService;
import com.pur.zsms.financing.vo.FinancingDetailVo;
import com.pur.zsms.financing.vo.FinancingManagerVo;
import com.pur.zsms.financing.vo.FinancingVo;
import com.pur.zsms.financing.vo.PictureVo;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.ordercustommanage.vo.OrderCustVo;
import com.pur.zsms.ordercustommanage.vo.SelectMaterVo;
import com.pur.zsms.pmsnmanage.role.vo.RoleVo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.pmsnmanage.user.po.UserRolePo;
import com.pur.zsms.pmsnmanage.user.service.IUserService;
import com.pur.zsms.pmsnmanage.user.vo.UserVo;
import com.pur.zsms.pmsnmanage.user.web.UserControllor;
import com.pur.zsms.shopmanage.service.IShopService;
import com.pur.zsms.shopmanage.vo.ShopVo;
import com.pur.zsms.supplier.po.ShopSupplierTypePo;
import com.pur.zsms.supplier.po.SupplierPo;
import com.pur.zsms.supplier.service.ISupplierService;
import com.pur.zsms.supplier.vo.ShopSupplierVo;
import com.pur.zsms.supplier.vo.SupplierVo;
import com.pur.zsms.systemsetting.datadictionary.po.Code;
import com.pur.zsms.systemsetting.datadictionary.service.IDataDictionaryService;
import com.pur.zsms.turnover.po.TurnoverPo;
import com.pur.zsms.turnover.po.TurnoverSubPo;
import com.pur.zsms.turnover.service.ITurnoverService;
import com.pur.zsms.turnover.vo.TurnoverVo;
import com.pur.zsms.unit.service.IUnitService;
import com.pur.zsms.unit.vo.UnitCommentVo;
import com.pur.zsms.unit.vo.UnitMenuVo;
import com.pur.zsms.unit.vo.UnitVo;

/**
 * 营业额
 * 
 * @author zhou
 * 
 */
@Controller
@RequestMapping(value = "/financingManager")
public class FinancingControllerManager {
	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(FinancingController.class);
	protected int page = 1;

	protected int pageSize = 15;

	PaginationSupport pageList;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Resource(name = "costService")
	private ICostService costService;

	@Resource(name = "turnoverService")
	private ITurnoverService turnoverService;

	@Resource(name = "financingService")
	private IFinancingService financingService;

	@Resource(name = "dictionaryService")
	private IDataDictionaryService dataDictionaryService;

	@Resource(name = "userService")
	private IUserService userService;

	@Resource(name = "shopService")
	private IShopService shopService;

	@RequestMapping(value = "/listFinancingListForManager.do")
	@ResponseBody
	public String listFinancingListForManager(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			FinancingManagerVo tFinancingManagerVo, HttpSession session) throws GenericException, IllegalAccessException, InvocationTargetException {
		JSONObject json = new JSONObject();

		rows = "100";

		if (tFinancingManagerVo.getInitFlag() == 0) {
			json.put("success", false);
			json.put("total", 0);
			return json.toString();
		}
		List<FinancingManagerVo> financingManagerVoList = new ArrayList<FinancingManagerVo>();
		// 集中采购成本
		FinancingManagerVo f1 = new FinancingManagerVo();
		f1.setFinancingType("集中采购成本");
		f1.setFinancingValue(financingService.getSelectMaterConcentration(tFinancingManagerVo));
		f1.setCustId(tFinancingManagerVo.getCustId());
		f1.setDateStart(tFinancingManagerVo.getDateStart());
		f1.setDateEnd(tFinancingManagerVo.getDateEnd());
		f1.setDetailContent("详细内容查看");
		f1.setDetailContentFlag("selectMaterConcentrationDetail");
		financingManagerVoList.add(f1);

		// 自采成本
		FinancingManagerVo f2 = new FinancingManagerVo();
		f2.setFinancingType("自采成本");
		f2.setFinancingValue(financingService.getSelectMaterSelf(tFinancingManagerVo));
		f2.setCustId(tFinancingManagerVo.getCustId());
		f2.setDateStart(tFinancingManagerVo.getDateStart());
		f2.setDateEnd(tFinancingManagerVo.getDateEnd());
		f2.setDetailContent("详细内容查看");
		f2.setDetailContentFlag("selectMaterSelfDetail");
		financingManagerVoList.add(f2);

		// 费用
		FinancingManagerVo f3 = new FinancingManagerVo();
		f3.setFinancingType("费用成本");
		f3.setFinancingValue(financingService.getCostAllValue(tFinancingManagerVo));
		f3.setCustId(tFinancingManagerVo.getCustId());
		f3.setDateStart(tFinancingManagerVo.getDateStart());
		f3.setDateEnd(tFinancingManagerVo.getDateEnd());
		f3.setDetailContent("详细内容查看");
		f3.setDetailContentFlag("costDetail");
		financingManagerVoList.add(f3);

		// 营业额
		FinancingManagerVo f4 = new FinancingManagerVo();
		f4.setFinancingType("营业额");
		f4.setFinancingValue(financingService.getTurnoverAllValue(tFinancingManagerVo));
		f4.setCustId(tFinancingManagerVo.getCustId());
		f4.setDateStart(tFinancingManagerVo.getDateStart());
		f4.setDateEnd(tFinancingManagerVo.getDateEnd());
		f4.setDetailContent("详细内容查看");
		f4.setDetailContentFlag("turnoverDetail");
		financingManagerVoList.add(f4);
		json.put("success", true);
		json.put("total", financingManagerVoList.size());
		json.put("rows", financingManagerVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	public String nullJudgeReturnString(Object object) {
		if (object == null || object.toString().equals("")) {
			return "0";
		}
		return object.toString();
	}

	public int nullJudgeReturnInt(Object object) {
		if (object == null || object.toString().equals("")) {
			return 0;
		}
		return Integer.valueOf(object.toString());
	}

	// 进货报表导出
	@RequestMapping(value = "/exportFinancing.do", method = { RequestMethod.POST })
	@ResponseBody
	public String exportFinancing(HttpServletRequest request, HttpServletResponse response, HttpSession session1, String orderDate,
			FinancingManagerVo tFinancingManagerVo) {
		JSONObject json = new JSONObject();
		HttpSession session = request.getSession();
		session.setAttribute("state", null);
		// 生成提示信息，
		String codedFileName = null;
		String codedFileNameUser = null;

		OutputStream fOut = null;
		UserPo userInfo = (UserPo) session1.getAttribute("user");

		try {

			// Map<Integer, String> letterMap = FileUtill.getLetterMap();

			Map<String, String> selfShopMap = new HashMap<String, String>();
			Map<String, String> concentrationShopMap = new HashMap<String, String>();
			Map<String, String> costShopMap = new HashMap<String, String>();
			Map<String, String> turnoverShopMap = new HashMap<String, String>();

			response.setContentType("application/vnd.ms-excel");
			codedFileName = new String("财务报表".getBytes("GBK"), "ISO8859-1");
			response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
			// response.addHeader("Content-Disposition",
			// "attachment;   filename=" + codedFileName + ".xls");
			// 产生工作簿对象
			HSSFWorkbook workbook = new HSSFWorkbook();
			// 产生工作表对象

			HSSFSheet sheet = workbook.createSheet();
			sheet.autoSizeColumn(1);
			sheet.autoSizeColumn(1, true);
			/*
			 * for (int i = 0; i < 50; i++) { sheet.setColumnWidth(i, 4000); }
			 */
			sheet.setColumnWidth(0, 3000);
			// 边框
			HSSFCellStyle setBorder = workbook.createCellStyle();
			setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
			setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
			setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
			setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
			setBorder.setAlignment(HSSFCellStyle.VERTICAL_CENTER);

			HSSFCellStyle setBorderLIGHT_Blue = workbook.createCellStyle();
			setBorderLIGHT_Blue.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
			setBorderLIGHT_Blue.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
			setBorderLIGHT_Blue.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
			setBorderLIGHT_Blue.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
			setBorderLIGHT_Blue.setAlignment(HSSFCellStyle.VERTICAL_CENTER);
			setBorderLIGHT_Blue.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
			setBorderLIGHT_Blue.setFillPattern(CellStyle.SOLID_FOREGROUND);

			HSSFCellStyle setBorderLIGHT_ORANGE = workbook.createCellStyle();
			setBorderLIGHT_ORANGE.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
			setBorderLIGHT_ORANGE.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
			setBorderLIGHT_ORANGE.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
			setBorderLIGHT_ORANGE.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
			setBorderLIGHT_ORANGE.setAlignment(HSSFCellStyle.VERTICAL_CENTER);
			setBorderLIGHT_ORANGE.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.getIndex());
			setBorderLIGHT_ORANGE.setFillPattern(CellStyle.SOLID_FOREGROUND);

			HSSFCellStyle setBorderORANGE = workbook.createCellStyle();
			setBorderORANGE.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
			setBorderORANGE.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
			setBorderORANGE.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
			setBorderORANGE.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
			setBorderORANGE.setAlignment(HSSFCellStyle.VERTICAL_CENTER);
			setBorderORANGE.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
			setBorderORANGE.setFillPattern(CellStyle.SOLID_FOREGROUND);

			HSSFCellStyle setBorderBLUE = workbook.createCellStyle();
			setBorderBLUE.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
			setBorderBLUE.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
			setBorderBLUE.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
			setBorderBLUE.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
			setBorderBLUE.setAlignment(HSSFCellStyle.VERTICAL_CENTER);
			setBorderBLUE.setFillForegroundColor(IndexedColors.BLUE.getIndex());
			setBorderBLUE.setFillPattern(CellStyle.SOLID_FOREGROUND);

			HSSFCellStyle setBorderLIME = workbook.createCellStyle();
			setBorderLIME.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
			setBorderLIME.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
			setBorderLIME.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
			setBorderLIME.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
			setBorderLIME.setAlignment(HSSFCellStyle.VERTICAL_CENTER);
			setBorderLIME.setFillForegroundColor(IndexedColors.LIME.getIndex());
			setBorderLIME.setFillPattern(CellStyle.SOLID_FOREGROUND);

			HSSFCellStyle setBorderGREY_25_PERCENT = workbook.createCellStyle();
			setBorderGREY_25_PERCENT.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
			setBorderGREY_25_PERCENT.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
			setBorderGREY_25_PERCENT.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
			setBorderGREY_25_PERCENT.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
			setBorderGREY_25_PERCENT.setAlignment(HSSFCellStyle.VERTICAL_CENTER);
			setBorderGREY_25_PERCENT.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
			setBorderGREY_25_PERCENT.setFillPattern(CellStyle.SOLID_FOREGROUND);

			HSSFCellStyle setBorderLIGHT_YELLOW = workbook.createCellStyle();
			setBorderLIGHT_YELLOW.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
			setBorderLIGHT_YELLOW.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
			setBorderLIGHT_YELLOW.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
			setBorderLIGHT_YELLOW.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
			setBorderLIGHT_YELLOW.setAlignment(HSSFCellStyle.VERTICAL_CENTER);
			setBorderLIGHT_YELLOW.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
			setBorderLIGHT_YELLOW.setFillPattern(CellStyle.SOLID_FOREGROUND);

			int sheetIndex = 0;
			// 初始行设置
			HSSFRow rowfist = sheet.createRow((int) 0);// 创建一行
			rowfist.setHeight((short) 340);// 行高

			int totalLine = 0;

			HSSFCell cellFirst1 = rowfist.createCell(totalLine);// 创建一列
			cellFirst1.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellFirst1.setCellValue("客户名称");
			cellFirst1.setCellStyle(setBorderGREY_25_PERCENT);

			totalLine = totalLine + 1;

			// 整理客户id和姓名
			List<Object[]> listUser = financingService.getUserIdAndUserName(tFinancingManagerVo.getCustIdAll());
			for (Object[] objects : listUser) {
				HSSFCell cellFirst = rowfist.createCell(totalLine);// 创建一列
				cellFirst.setCellType(HSSFCell.CELL_TYPE_STRING);
				cellFirst.setCellValue(objects[1].toString());
				cellFirst.setCellStyle(setBorderGREY_25_PERCENT);

				int width = objects[1].toString().getBytes().length * 256;
				if (width > 3000) {
					sheet.setColumnWidth(totalLine, width);
				} else {
					sheet.setColumnWidth(totalLine, 3000);
				}
				totalLine = totalLine + 1;
			}
			// 创建种类属性列

			HSSFRow rowfist2 = sheet.createRow((int) 1);// 创建一行
			rowfist2.setHeight((short) 340);// 行高

			HSSFCell cellFist21 = rowfist2.createCell(0);// 创建一列
			cellFist21.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellFist21.setCellValue("集中采购");
			cellFist21.setCellStyle(setBorderLIGHT_YELLOW);
			// 设置各个店的集中采购数额
			int totalLineFist2 = 1;
			for (Object[] objects : listUser) {
				tFinancingManagerVo.setCustId(objects[0].toString());
				HSSFCell cellFist2 = rowfist2.createCell(totalLineFist2);// 创建一列
				cellFist2.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
				String concentrationValue = financingService.getSelectMaterConcentration(tFinancingManagerVo);
				cellFist2.setCellValue(concentrationValue);
				cellFist2.setCellStyle(setBorderLIGHT_YELLOW);
				concentrationShopMap.put(objects[0].toString(), concentrationValue);

				totalLineFist2 = totalLineFist2 + 1;

			}

			HSSFRow rowfist3 = sheet.createRow((int) 2);// 创建一行
			rowfist2.setHeight((short) 340);// 行高
			HSSFCell cellFist31 = rowfist3.createCell(0);// 创建一列
			cellFist31.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellFist31.setCellValue("自采");
			cellFist31.setCellStyle(setBorderLIGHT_YELLOW);
			// 设置各个店的自采数额
			int totalLineFist3 = 1;
			for (Object[] objects : listUser) {
				tFinancingManagerVo.setCustId(objects[0].toString());
				HSSFCell cellFist3 = rowfist3.createCell(totalLineFist3);// 创建一列
				cellFist3.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
				String selfValue = financingService.getSelectMaterSelf(tFinancingManagerVo);
				cellFist3.setCellValue(selfValue);
				cellFist3.setCellStyle(setBorderLIGHT_YELLOW);

				selfShopMap.put(objects[0].toString(), selfValue);
				totalLineFist3 = totalLineFist3 + 1;

			}
			// 其他费用
			List<Code> codeList = dataDictionaryService.getCodeList("costType");
			int rowCostline = 3;
			for (Code code : codeList) {
				HSSFRow rowCost = sheet.createRow(rowCostline);// 创建一行
				rowCost.setHeight((short) 340);// 行高
				HSSFCell cellCost = rowCost.createCell(0);// 创建一列
				cellCost.setCellType(HSSFCell.CELL_TYPE_STRING);
				cellCost.setCellValue(code.getCodeName());
				cellCost.setCellStyle(setBorderLIGHT_Blue);

				int totalLineCost = 1;
				for (Object[] objects : listUser) {
					tFinancingManagerVo.setCostType(Integer.valueOf(code.getCodeId()));
					tFinancingManagerVo.setCustId(objects[0].toString());
					HSSFCell cellcellCostFist = rowCost.createCell(totalLineCost);// 创建一列
					cellcellCostFist.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
					cellcellCostFist.setCellValue(financingService.getCostAllValueGroupByCostType(tFinancingManagerVo));
					cellcellCostFist.setCellStyle(setBorderLIGHT_Blue);
					totalLineCost = totalLineCost + 1;
				}

				rowCostline = rowCostline + 1;
			}
			// 其他费用总和

			HSSFRow rowCostAll = sheet.createRow(rowCostline);// 创建一行
			rowCostAll.setHeight((short) 340);// 行高
			HSSFCell cellCostAllfist = rowCostAll.createCell(0);// 创建一列
			cellCostAllfist.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellCostAllfist.setCellValue("其他费用总和");
			cellCostAllfist.setCellStyle(setBorderBLUE);
			// 设置各个店的其他费用总和
			int totalLineCostAll = 1;
			for (Object[] objects : listUser) {
				tFinancingManagerVo.setCustId(objects[0].toString());
				HSSFCell cellFist3 = rowCostAll.createCell(totalLineCostAll);// 创建一列
				cellFist3.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
				String costShopValue = financingService.getCostAllValue(tFinancingManagerVo);
				cellFist3.setCellValue(costShopValue);
				cellFist3.setCellStyle(setBorderBLUE);
				costShopMap.put(objects[0].toString(), costShopValue);
				totalLineCostAll = totalLineCostAll + 1;

			}
			rowCostline = rowCostline + 1;
			// 营业额

			List<Code> codeList2 = dataDictionaryService.getCodeList("turnoverType");
			for (Code code : codeList2) {
				HSSFRow rowTurnover = sheet.createRow(rowCostline);// 创建一行
				rowTurnover.setHeight((short) 340);// 行高
				HSSFCell cellTurnover = rowTurnover.createCell(0);// 创建一列
				cellTurnover.setCellType(HSSFCell.CELL_TYPE_STRING);
				cellTurnover.setCellValue(code.getCodeName());
				cellTurnover.setCellStyle(setBorderLIGHT_ORANGE);

				int totalLineTurnover = 1;
				for (Object[] objects : listUser) {
					tFinancingManagerVo.setTurnoverType(Integer.valueOf(code.getCodeId()));
					tFinancingManagerVo.setCustId(objects[0].toString());
					HSSFCell cellcellTurnoverFist = rowTurnover.createCell(totalLineTurnover);// 创建一列
					cellcellTurnoverFist.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
					cellcellTurnoverFist.setCellValue(financingService.getTurnoverAllValueGroupByTurnoverType(tFinancingManagerVo));
					cellcellTurnoverFist.setCellStyle(setBorderLIGHT_ORANGE);
					totalLineTurnover = totalLineTurnover + 1;
				}

				rowCostline = rowCostline + 1;

			}

			// 其他费用总和

			HSSFRow rowTurnoverAll = sheet.createRow(rowCostline);// 创建一行
			rowTurnoverAll.setHeight((short) 340);// 行高
			HSSFCell cellTurnoverAllfist = rowTurnoverAll.createCell(0);// 创建一列
			cellTurnoverAllfist.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellTurnoverAllfist.setCellValue("营业额总和");
			cellTurnoverAllfist.setCellStyle(setBorderORANGE);
			// 设置各个店的其他费用总和
			int totalLineTurnoverAll = 1;
			for (Object[] objects : listUser) {
				tFinancingManagerVo.setCustId(objects[0].toString());
				HSSFCell cellFist3 = rowTurnoverAll.createCell(totalLineTurnoverAll);// 创建一列
				cellFist3.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
				String turnoverShopValue = financingService.getTurnoverAllValue(tFinancingManagerVo);

				cellFist3.setCellValue(turnoverShopValue);
				cellFist3.setCellStyle(setBorderORANGE);
				turnoverShopMap.put(objects[0].toString(), turnoverShopValue);
				totalLineTurnoverAll = totalLineTurnoverAll + 1;

			}
			rowCostline = rowCostline + 1;

			// 净利润
			HSSFRow rowProfit = sheet.createRow(rowCostline);// 创建一行
			rowProfit.setHeight((short) 340);// 行高
			HSSFCell cellProfitfist = rowProfit.createCell(0);// 创建一列
			cellProfitfist.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellProfitfist.setCellValue("净利润");
			cellProfitfist.setCellStyle(setBorderLIME);
			// 设置各个店的净利润
			int totalLineProfit = 1;
			for (Object[] objects : listUser) {
				tFinancingManagerVo.setCustId(objects[0].toString());
				HSSFCell cellFist3 = rowProfit.createCell(totalLineProfit);// 创建一列
				cellFist3.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
				BigDecimal profitvalue = new BigDecimal(turnoverShopMap.get(objects[0].toString()))
						.subtract(new BigDecimal(costShopMap.get(objects[0].toString())))
						.subtract(new BigDecimal(selfShopMap.get(objects[0].toString())))
						.subtract(new BigDecimal(concentrationShopMap.get(objects[0].toString())));
				cellFist3.setCellValue(profitvalue.toString());
				cellFist3.setCellStyle(setBorderLIME);
				totalLineProfit = totalLineProfit + 1;

			}
			rowCostline = rowCostline + 1;

			// 毛利率
			HSSFRow rowGross = sheet.createRow(rowCostline);// 创建一行
			rowGross.setHeight((short) 340);// 行高
			HSSFCell cellGrossfist = rowGross.createCell(0);// 创建一列
			cellGrossfist.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellGrossfist.setCellValue("毛利率");
			cellGrossfist.setCellStyle(setBorderLIME);
			// 设置各个店的毛利率
			int totalLineGross = 1;
			for (Object[] objects : listUser) {
				tFinancingManagerVo.setCustId(objects[0].toString());
				HSSFCell cellFist3 = rowGross.createCell(totalLineGross);// 创建一列
				cellFist3.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
				BigDecimal profitvalue = new BigDecimal(turnoverShopMap.get(objects[0].toString()))
						.subtract(new BigDecimal(costShopMap.get(objects[0].toString())))
						.subtract(new BigDecimal(selfShopMap.get(objects[0].toString())))
						.subtract(new BigDecimal(concentrationShopMap.get(objects[0].toString())));
				BigDecimal grossvalue = new BigDecimal(0);
				if (profitvalue.compareTo(grossvalue) > 0) {
					grossvalue = profitvalue.divide(new BigDecimal(turnoverShopMap.get(objects[0].toString())), 2, BigDecimal.ROUND_HALF_UP);
				}
				cellFist3.setCellValue(grossvalue.toString());
				cellFist3.setCellStyle(setBorderLIME);
				totalLineGross = totalLineGross + 1;

			}
			rowCostline = rowCostline + 1;

			workbook.setSheetName(sheetIndex, "财务分析报表");

			sheet.setForceFormulaRecalculation(true);
			fOut = response.getOutputStream();
			workbook.write(fOut);
		} catch (UnsupportedEncodingException e1) {
		} catch (Exception e) {
		} finally {
			try {
				fOut.flush();
				fOut.close();
			} catch (IOException e) {
				json.put("success", false);
			}
			session.setAttribute("state", "open");
		}

		json.put("success", true);
		return json.toString();
	}

	@RequestMapping(value = "/auditFinancingList.do")
	@ResponseBody
	public String auditFinancingList(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			FinancingVo tFinancingVo, HttpSession session) throws GenericException, IllegalAccessException, InvocationTargetException {
		JSONObject json = new JSONObject();
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";

		if (tFinancingVo.getInitFlag() == 0) {
			json.put("success", false);
			json.put("total", 0);
			return json.toString();
		}
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = financingService.auditFinancingList(pageController, tFinancingVo);

		List items = pageList.getItems();
		List<FinancingVo> financingVoList = new ArrayList<FinancingVo>();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				FinancingVo f = new FinancingVo();
				f.setTurnoverId(Long.valueOf(nullJudgeReturnString(item[0])));
				if (item[1] != null && !item[1].equals("")) {
					f.setCustId(Long.valueOf(nullJudgeReturnString(item[1])));
				}
				if (item[2] != null && !item[2].equals("")) {
					f.setCustName(nullJudgeReturnString(item[2]));
				}
				f.setTurnoverValueTotal(nullJudgeReturnString(item[3]));
				f.setTurnoverTypeDetialValue(nullJudgeReturnString(item[4]));
				f.setTurnoverState(nullJudgeReturnInt(item[5]));
				if (item[6] != null && !item[6].equals("")) {
					f.setDate(item[6].toString());
				}

				f.setCostId(Long.valueOf(nullJudgeReturnString(item[7])));
				if (item[8] != null && !item[8].equals("")) {
					f.setCustId(Long.valueOf(nullJudgeReturnString(item[8])));
				}
				if (item[9] != null && !item[9].equals("")) {
					f.setCustName(nullJudgeReturnString(item[9]));
				}
				f.setCostValueTotal(nullJudgeReturnString(item[10]));
				f.setCostTypeDetialValue(nullJudgeReturnString(item[11]));
				f.setCostState(nullJudgeReturnInt(item[12]));
				if (item[13] != null && !item[13].equals("")) {
					f.setDate(item[13].toString());
				}
				financingVoList.add(f);
			}
		}

		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", financingVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/auditFinancing.do", method = { RequestMethod.POST })
	@ResponseBody
	public String auditFinancing(FinancingVo tFinancingVo, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			financingService.auditFinancing(tFinancingVo);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		json.put("success", true);
		return json.toString();
	}

	@RequestMapping(value = "/goFinancingDetail.do", method = { RequestMethod.GET })
	public ModelAndView goFinancingDetail(FinancingManagerVo tFinancingManagerVo, HttpSession session) {
		Map map = new HashMap();
		try {
			ShopVo shopVo = shopService.getShopVoByShopId(Long.valueOf(tFinancingManagerVo.getCustId()));
			tFinancingManagerVo.setCustName(shopVo.getShopName());
		} catch (GenericException e) {
			e.printStackTrace();
		}
		if (tFinancingManagerVo.getDetailContentFlag().equals("selectMaterConcentrationDetail")) {
			tFinancingManagerVo.setSupway("集中采购");
			tFinancingManagerVo.setSupwayFlag(1);
			map.put("FinancingManagerVo", tFinancingManagerVo);
			return new ModelAndView("/financing/detail/selectMaterConcentrationDetail", map);
		} else if (tFinancingManagerVo.getDetailContentFlag().equals("selectMaterSelfDetail")) {
			tFinancingManagerVo.setSupway("自采");
			tFinancingManagerVo.setSupwayFlag(2);
			map.put("FinancingManagerVo", tFinancingManagerVo);
			return new ModelAndView("/financing/detail/selectMaterConcentrationDetail", map);
		} else if (tFinancingManagerVo.getDetailContentFlag().equals("costDetail")) {
			map.put("FinancingManagerVo", tFinancingManagerVo);
			return new ModelAndView("/financing/detail/costDetailForManager", map);
		} else if (tFinancingManagerVo.getDetailContentFlag().equals("turnoverDetail")){
			map.put("FinancingManagerVo", tFinancingManagerVo);
			return new ModelAndView("/financing/detail/turnoverDetailForManager", map);
		}
		//map.put("FinancingManagerVo", tFinancingManagerVo);

		return new ModelAndView("/", map);
	}

	@RequestMapping(value = "/selectMaterConcentrationDetail.do", method = { RequestMethod.POST })
	@ResponseBody
	public String selectMaterConcentrationDetail(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			FinancingDetailVo tFinancingDetailVo) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		List<FinancingDetailVo> financingDetailVoList = new ArrayList<FinancingDetailVo>();
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = financingService.findSelectMaterConcentrationDetail(pageController, tFinancingDetailVo);
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				FinancingDetailVo f = new FinancingDetailVo();
				f.setOrdId((Integer) item[0]);
				f.setOrdCustId((Integer) item[1]);
				f.setOrdCustName((String) item[2]);
				f.setOrdDate((String) item[3]);
				f.setMaterId((Integer) item[4]);
				f.setMaterName((String) item[5]);
				f.setSpecifications((String) item[6]);
				f.setBrands((String) item[7]);
				f.setOrdMaterWeight((BigDecimal) item[8]);
				f.setUnit((String) item[9]);
				f.setOrdMaterValue((BigDecimal) item[10]);
				if (item[10] == null || item[8] == null) {
					f.setOrdMaterValueEvery(new BigDecimal(0));
				} else {
					f.setOrdMaterValueEvery(((BigDecimal) item[10]).divide((BigDecimal) item[8], 2, BigDecimal.ROUND_HALF_UP));
				}

				f.setSupId((Integer) item[11]);
				f.setSupName((String) item[12]);

				financingDetailVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", financingDetailVoList);
		return json.toString();
	}
}
